package com.example.newsclient.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.newsclient.DetailedActivity;
import com.example.newsclient.MainActivity;
import com.example.newsclient.R;
import com.example.newsclient.bean.Top;
import com.example.newsclient.tool.NetUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 袁永杰 on 2019/5/30.
 * 头条新闻列表适配器
 */

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> implements View.OnClickListener{

    private List<Top.ResultBean.DataBean> dataBeanList;
    private Context context;
    private List<Bitmap> imgUrlList;
    private RecyclerView recyclerView;

    public MyRecyclerAdapter(List<Top.ResultBean.DataBean> dataBeanList,List<Bitmap> imgUrlList, Context context){
        this.dataBeanList = dataBeanList;
        this.imgUrlList = imgUrlList;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.news_item,null);
        MyViewHolder myViewHolder = new MyViewHolder(itemView);
        itemView.setOnClickListener(this);
        recyclerView = (RecyclerView) parent;
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        Top.ResultBean.DataBean dataBean = dataBeanList.get(position);
        holder.tv_news_title.setText(dataBean.getTitle());
        holder.tv_author.setText(dataBean.getAuthor_name());
        holder.tv_date.setText(dataBean.getDate());
        holder.iv_news.setImageBitmap(imgUrlList.get(position));
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    /**
     * 注册点击事件
     * @param view
     */
    @Override
    public void onClick(View view) {
        int position = recyclerView.getChildLayoutPosition(view);
        //点击相应的子项，显示详细新闻信息
        Intent intent = new Intent(context, DetailedActivity.class);
        intent.putExtra("news_url",dataBeanList.get(position).getUrl());
        context.startActivity(intent);
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView tv_news_title;
        TextView tv_author;
        TextView tv_date;
        ImageView iv_news;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_news_title = itemView.findViewById(R.id.tv_news_title);
            tv_author = itemView.findViewById(R.id.tv_author);
            tv_date = itemView.findViewById(R.id.tv_date);
            iv_news = itemView.findViewById(R.id.iv_news);
        }
    }
}
